package mailsystem.persistance;

import java.util.List;

import mailsystem.Mailbox;
import mailsystem.Message;

public abstract class MailsystemPersistance {
	protected MailBoxPersistance mailboxes;
	protected ContactPersistance contacts;
	protected NewMessagePersistance newMessages;
	protected KeptMessagePersistance keptMessages;
	private int mailboxCount;
	
	public MailsystemPersistance(int theMailboxCount)
	{
		init();		
		mailboxCount = theMailboxCount;
		
		if ( mailboxes.listAll().size() == 0 )
		{
			createNewMailBoxes();
		}
	}
	
	public abstract void init();
	
	public void addMailbox(Mailbox box)
	{
		mailboxes.add(box);
	}
	
	public void deleteMailbox(Mailbox box)
	{
		mailboxes.delete(box);
	}
	
	public void updateMailbox(Mailbox box)
	{
		mailboxes.update(box);
	}
	
	public List<Mailbox> listAllMailbox()
	{
		return mailboxes.listAll();
	}
	
	public void addContact(Mailbox box, Mailbox contact)
	{
		contacts.add(box, contact);
	}
	
	public void deleteContact(Mailbox box, Mailbox contact)
	{
		contacts.delete(box, contact);
	}
	
	public List<Mailbox> getAllContacts(Mailbox box)
	{
		return contacts.listAll(box);
	}
	
	public void addNewMessage(Mailbox box, Message message)
	{
		newMessages.add(box, message);
	}
	
	public void deleteNewMessage(Message message)
	{
		newMessages.delete(message);
	}
	
	public List<Message> listAllNewMessages(Mailbox box)
	{
		return newMessages.listAll(box);
	}
	
	public void addKeptMessage(Mailbox box, Message message)
	{
		keptMessages.add(box, message);
	}
	
	public void deleteKeptMessage(Message message)
	{
		keptMessages.delete(message);
	}
	
	public List<Message> listAllKeptMessages(Mailbox box)
	{
		return keptMessages.listAll(box);
	}	
	
	private void createNewMailBoxes()
	{
		for (int i = 0; i < mailboxCount; i++) {
			String passcode = "" + (i + 1);
			String name = "Mailbox " + (i + 1);
			String greeting = "audio/reached.wav";
			Mailbox mailbox = new Mailbox(i, passcode, greeting, name, this);
			mailboxes.add(mailbox);
		}
	}
}
